from django.shortcuts import render

from rest_framework.viewsets import ReadOnlyModelViewSet
from areas.models import Areas

from areas.serializers import AreaSerializer, SubAreaSerializer


class AddressViewSet(ReadOnlyModelViewSet):
    # Create your views here.
    # 1.get请求 获取第一栏的没有父级单位的地区名称
    # 2.get请求 获取与第一栏相关的所有地区名称
    # serializer_class = xxx
    # queryset = xxx
    # action不同 查询集获取的信息来源不同 重写get_queryset方法
    def get_queryset(self):
        if self.action == 'list':
            return Areas.objects.filter(parent=None)
        else:
            return Areas.objects.all()

    def get_serializer_class(self):
        if self.action == 'list':
            return AreaSerializer
        else:
            return SubAreaSerializer